//
//  RetrieveView.m
//  xpkc
//
//  Created by TopSage iOS on 15/3/9.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "RetrieveView.h"

@interface RetrieveView (){
    UITextField *emailText;
    UIButton *submitBtn;
}

@end

@implementation RetrieveView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 创建视图
        [self creatRetrieveView];
    }
    return self;
}
//创建视图
- (void)creatRetrieveView{
    self.backgroundColor = BGColor;
    
    //放置登录框等的基本信息
    CGRect frame_email;
    CGRect frame_msg;
    CGRect frame_submit;
    CGFloat offset = 0.04*SCREEN_WIDTH;
    if (isPad) {

    }else{
        frame_email = CGRectMake(offset, 40, SCREEN_WIDTH - offset*2, 50);
        frame_msg = CGRectMake(offset, 90, SCREEN_WIDTH - offset*2, 50);
        frame_submit = CGRectMake(offset, 150, SCREEN_WIDTH - offset*2, 50);
    }
    emailText = [[UITextField alloc]initWithFrame:frame_email];
    emailText.textColor = [UIColor darkGrayColor];
    emailText.backgroundColor = [UIColor whiteColor];
    emailText.placeholder = @"  请输入注册使用的邮箱地址";
    emailText.layer.borderWidth = 1;
    emailText.layer.borderColor = [UIColor lightGrayColor].CGColor;
    emailText.delegate = self;
    emailText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self addSubview:emailText];

    if (isPad) {
        emailText.font = FONT_SYS_BIG;
        submitBtn.titleLabel.font = FONT_SYS_BIG;
    }

    
    UILabel *msg = [[UILabel alloc]initWithFrame:frame_msg];
    msg.text = @"提交后系统将该邮箱发送一封找回密码邮件，请打开邮箱完成后续步骤。";
    msg.font = [UIFont systemFontOfSize:14];
    msg.textColor = [UIColor darkGrayColor];
    msg.numberOfLines = 0;
    [self addSubview:msg];
    
    
    submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.frame = frame_submit;
    submitBtn.backgroundColor = OrangeColor;
    [submitBtn setTitle:@"提交申请" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [submitBtn setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    submitBtn.titleLabel.shadowOffset = CGSizeMake(1, 0.5);
    [submitBtn.layer setCornerRadius:1];
    [submitBtn addTarget:self action:@selector(buttonClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:submitBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resigntKeyboard)];
    [self addGestureRecognizer:tap];

}

-(void)buttonClick
{
    NSString *module = @"user";
    NSString *interface = @"retripasswd";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"email":emailText.text};
    
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([dic objectForKey:@"succ"]) {
            ShowAlertMsg(@"发送成功,请注意查收");
        }
        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ShowAlertMsg(@"发送失败");
    }];

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [emailText resignFirstResponder];
    return YES;
}

- (void)resigntKeyboard{
    [emailText resignFirstResponder];
}

- (void)submitBtnClick:(UIButton *)btn{
    [self localEmailCheck];
}
- (void)localEmailCheck{
    if (1) {
        //
        if ([self.delegate respondsToSelector:@selector(retrieveWithEmail:)]) {
            [self.delegate retrieveWithEmail:emailText.text];
        }
    }
}


@end